package com.e.takeaway;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.e.takeaway.adapter.PayAdapter;
import com.e.takeaway.po.Food;
import com.e.takeaway.po.FoodVo;
import com.e.takeaway.service.RequestService;
import com.e.takeaway.util.MyRetrofit;
import com.google.gson.Gson;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class PayActivity extends AppCompatActivity {
    private static final String FILE_NAME = "user";
    private TextView tv_shopname,tv_time,tv_uer,tv_sum_price,tv_pay;
    private static BigDecimal bigprice = new BigDecimal(0.00);
    private PayAdapter payAdapter;
    private ListView listView;
    static int snumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        getSupportActionBar().hide();
        InitView();
    }


    @SuppressLint("SetTextI18n")
    private void InitView(){
        listView = (ListView)findViewById(R.id.list_food);
        tv_shopname = (TextView) findViewById(R.id.pay_shop_name);
        tv_time = (TextView) findViewById(R.id.pay_order_time);
        tv_uer = (TextView) findViewById(R.id.pay_user);
        tv_pay = (TextView)findViewById(R.id.order_pay_tv);
        //支付

        tv_sum_price = (TextView)findViewById(R.id.order_pay_price);
        Bundle b  = getIntent().getExtras();
        String shopimg = b.getString("shopimg");
        tv_shopname.setText(b.getString("shopname"));
        tv_time.setText(b.getString("time"));
        SharedPreferences sp = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String phone = sp.getString("phone","");
        String nick = sp.getString("nickname","");
        tv_uer.setText(nick+"先生    "+phone.substring(0,3)+"****"+phone.substring(7));
        ArrayList list = b.getParcelableArrayList("myfood");
        List<Food> foods = (List<Food>)list.get(0);

        for (int i = 0; i < foods.size(); i++) {
            bigprice = bigprice.add(foods.get(i).getSum());
            snumber = snumber+foods.get(i).getNumber();
        }
        tv_sum_price.setText(bigprice.toString());
        payAdapter = new PayAdapter(this,foods);
        listView.setAdapter(payAdapter);
        tv_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HashMap map = new HashMap();
                map.clear();
                List<FoodVo> foodVos = new ArrayList<>();
                for (int i = 0; i <foods.size() ; i++) {
                    FoodVo foodVo = new FoodVo();
                    foodVo.setFoodId(String.valueOf(foods.get(i).getFoodId()));
                    foodVo.setNumber(foods.get(i).getNumber());
                    foodVos.add(foodVo);
                }
                System.out.println(foodVos.toString());
                map.put("phone",phone);
                map.put("address","成都文理学院6栋-149");
                map.put("name",nick);
                map.put("status",1);
                map.put("isdelete",0);
                map.put("foods",foodVos);
                map.put("sum",bigprice.toString());
                map.put("snumber",snumber);
                map.put("merimg",shopimg);
                map.put("mername",tv_shopname.getText().toString());
                System.out.println(map.toString());
                Retrofit retrofit = MyRetrofit.getRetrofit();
                RequestService request = retrofit.create(RequestService.class);
                Call<Integer> call = request.pay(map);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        if(response.body()==1){
                            System.out.println("------------------------------------------购买成功");
                            Intent intent = new Intent(PayActivity.this,MainActivity.class);
                            Bundle b = new Bundle();
                            b.putInt("flag",1);
                            intent.putExtras(b);
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {

                    }
                });
            }
        });
    }
}